package com.chen.demo.rhombus.acl.adapter.repository;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.demo.rhombus.acl.adapter.dao.UserDao;
import com.chen.demo.rhombus.acl.adapter.mapper.UserMapper;
import com.chen.demo.rhombus.acl.port.repository.UserRepository;
import com.chen.demo.rhombus.domain.entity.UserDo;
import com.chen.demo.rhombus.domain.enums.Type;
import com.chen.demo.rhombus.domain.vo.UserVo;
import com.chen.demo.rhombus.ohs.local.pl.command.query.UserPageQuery;
import com.chen.winnerframework.util.PageUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class UserRepositoryImpl implements UserRepository {

    private final UserDao userDao;
    private final UserMapper userMapper;

    @Override
    public void insertUser(UserDo userDO) {
        userDao.save(userDO);
    }

    @Override
    public Page<UserVo> selectUserByPage(UserPageQuery pageQuery) {
        Page<UserDo> page = PageUtil.build(pageQuery);
        String username = pageQuery.getQuery().getUsername();
        Type type = pageQuery.getQuery().getType();
        LambdaQueryWrapper<UserDo> lwq = Wrappers.<UserDo>lambdaQuery()
                .like(StringUtils.isNotBlank(username), UserDo::getUsername, username)
                .eq(ObjectUtil.isNotNull(type), UserDo::getType, type);
        return userMapper.selectVoPage(page, lwq);
    }
}
